/*
 *  Class name: EditablePlayerList.java
 *  Version: 1.0
 *  Date: 11.12.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.entity;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.PlayerDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.TeamDTO;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.PageableListPanel;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.items.PageableListItemButton;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.items.entity.EditablePlayerListItem;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.dialogs.PlayerDetailDialogWindow;
import cz.muni.fi.pa165.soccergamesarchive.restclient.rest.EntityRestClient;
import cz.muni.fi.pa165.soccergamesarchive.restclient.rest.entity.PlayerRestClient;
import java.awt.Dialog;
import java.awt.Window;
import java.util.List;

/**
 *
 * @author Michal Ďuriš
 */
public class EditablePlayerList extends PageableListPanel<PlayerDTO> {

    private static final long serialVersionUID = 1L;

    private PageableListItemButton<TeamDTO> parentEntityItem;

    public EditablePlayerList() {
        super();
    }

    public EditablePlayerList(Window owner, String panelTitle,
                              PageableListItemButton<TeamDTO> parentEntityItem) {
        super(owner, panelTitle);

        this.parentEntityItem = parentEntityItem;
    }

    @Override
    public void init() {
        init(null);
    }

    @Override
    public void init(List<PlayerDTO> entityList) {
        initializeList(entityList);
    }

    @Override
    public PageableListItemButton<PlayerDTO> createEntityListItem() {

        PlayerDTO entity = new PlayerDTO();

        if (parentEntityItem != null) {
            entity.setTeam(parentEntityItem.getEntity());
        }

        return createEntityListItem(entity);
    }

    @Override
    public PageableListItemButton<PlayerDTO> createEntityListItem(
            PlayerDTO entity) {

        EditablePlayerListItem newItem = new EditablePlayerListItem(this, entity);
        newItem.init();

        return newItem;
    }

    @Override
    protected EntityRestClient<PlayerDTO> getRestClientImplementation() {
        return new PlayerRestClient();
    }

    @Override
    protected void handleAddItemAction() {

        new PlayerDetailDialogWindow(parentEntityItem, createEntityListItem(),
                Dialog.ModalityType.APPLICATION_MODAL).init().setVisible(true);
    }

    public PageableListItemButton<TeamDTO> getParentEntityItem() {
        return parentEntityItem;
    }

}
